package customer.ejb.eaos;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import common.Filter;
import common.IPredicate;
import model.GetEntities;
import model.Hotel;
import authentication.ejb.usermanagement.User;
import dtos.HotelDTO;
import dtos.UserDTO;


@Stateless
public class HotelListEAO implements HotelList{
	

	@PersistenceContext
    private EntityManager em;
	
	private String place;
	
	
	public HotelListEAO(){
		
	}
	
	@Override
	public void add(HotelDTO hotelDTO) {
		GetEntities ge = new GetEntities();
		em.persist(ge.getHotelEntity(hotelDTO));
		
	}

	@Override
	public Collection<HotelDTO> getHotelList(final String place) {
		this.place = place;
		

		@SuppressWarnings("unchecked")
		List<Hotel> qres = em.createQuery("SELECT x FROM Hotel x").getResultList();
		Collection<Hotel> fres = Filter.filter(qres, isOK);
		Collection<HotelDTO> res = new ArrayList<HotelDTO>(fres.size());
		
		
		for(Hotel e : fres)
		{
			res.add(e.getDTO());
		}
		
		return res;
		
	}
	
	IPredicate<Hotel> isOK = new IPredicate<Hotel>() {
		public boolean apply(Hotel hotel) {
	        // binds a boolean method in User to a reference
	        return hotel.isAtPlace(place);
	    }

	};
	

	@Override
	public HotelDTO getHotelID(int id) {
		// TODO Auto-generated method stub
		return null;
	}

}
